<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

/**
 *  Home Class
 *
 * @package XGO CMS v2.0
 * @subpackage News
 * @author Manh Nguyen - 30/07/2012
 * @link http://sunsoft.vn
 */
class Home extends CI_Controller {

    private $client_ip;

    /**
     * Contrust class
     *
     * @access public
     */
    public function __construct() {
        parent::__construct();

        $this->load->database();
        $this->load->library('datamapper');
        $this->load->library('smarty3', '', 'smarty');
        $this->smarty = new CI_Smarty3();
        $this->load->library('session');
        
        $this->client_ip = getIP();
        
        require_once APPPATH.'modules/frontend/common/main_menu.php';
        require_once APPPATH.'modules/frontend/common/slide_new_item.php';
        require_once APPPATH.'modules/frontend/common/best_selling_slide.php';
        require_once APPPATH.'modules/frontend/common/footer.php';
        require_once APPPATH.'modules/frontend/common/viewed_product.php';
        require_once APPPATH.'modules/frontend/common/top.php';
    }

    public function index() {
//        echo $salt = md5(uniqid(rand(), TRUE));
//        echo "<br>";
//        echo sha1($salt."admin12");exit;
//echo $_SERVER['DOCUMENT_ROOT'];exit;
        
        $news = new News_model();
        $category = new Category_model();
        
        //$limit = 40;
        $catid = array('219','220','222','221'); //trang sức bạc, túi xách phụ kiện, mẹ và bé, nội thất trang trí
        $allcat=array();
        foreach($catid as $id)
        {  
            foreach($category->get_list_category_child($id) as $c)
            {
                array_push($allcat, $c->id);
            }
        }
        
        $paging['cur-page'] = 1;
        $paging['total-item'] = $news->count_by_category($allcat);
        $paging['item-per-page'] = 20;
        $data['item'] = $news->get_last_news_page($allcat, $paging['item-per-page'], 1);
       
        $this->session->set_flashdata('redirect', $this->uri->uri_string());
        $this->smarty->assign(array(
                                'data'=>$data,
                                'paging'=>$paging,
                                'type' => 'all',
                                //'itemviewed'=>$arrviewed
                                ));
        $this->smarty->display('template/frontend/home');
    }
    
    function pagination()
    {
        $news = new News_model();
        $category = new Category_model();
        
        $type = explode('_',$this->input->post('type'));
        $paging['cur-page'] = (int)$this->input->post('p');
        
        $paging['item-per-page'] = 20;
        
        if($type[0]=='search')
        {
            $keyword = $type[1];
        
            $news = new News_model();     
            $data['item'] = $news->get_search($keyword,$paging['item-per-page'],$paging['cur-page']);
            $paging['total-item'] = count($news->get_search($keyword));
        }
        else
            if($type[0] == 'all')
            {
                $catid = array('219','220','222','221'); //trang sức bạc, túi xách phụ kiện, mẹ và bé, nội thất trang trí
                $allcat=array();
                foreach($catid as $id)
                {  
                    foreach($category->get_list_category_child($id) as $c)
                    {
                        array_push($allcat, $c->id);
                    }
                }
                $paging['total-item'] = $news->count_by_category($allcat);
                $data['item'] = $news->get_last_news_page($allcat, $paging['item-per-page'], $paging['cur-page']);
            }
            else
            {//echo $type[0];exit;
                $category->where('code',$type[0]);
                $category->get(1);

                $data['item'] = $news->get_by_category_code($type[0], $paging['item-per-page'], $paging['cur-page']);
                $paging['total-item'] = $news->count_by_category(array("$category->id"));
            }
        
        $this->smarty->assign(array(
                                'data'=>$data,
                                'paging'=>$paging,
                                'type' => $type[0]
                                ));
        $this->smarty->display('frontend/subs/list_product');
    }
    
    public function search()
    {
        $keyword = $this->input->post('keyword');
        
        $news = new News_model();     
        
        $paging['cur-page'] = 1;
        $paging['total-item'] = count($news->get_search($keyword));
        $paging['item-per-page'] = 20;
        $data['item'] = $news->get_search($keyword, $paging['item-per-page'], 1);
        
        $this->smarty->assign(array('data'=>$data,'paging'=>$paging,'type'=>'search_'.$keyword,'keyword'=>$keyword));
        $this->smarty->display('template/frontend/home');
    }
    
    public function remove_viewed()
    {
        $strip = str_replace(".", "-", $this->client_ip);
        if(isset($_COOKIE["$strip-itemviewed"]))
        {
            setcookie("$strip-itemviewed","", time()-10,'/svn/',$_SERVER['SERVER_NAME']);//print_r($_COOKIE);exit;
            //unset($_COOKIE["$strip-itemviewed"]);
            //redirect('home');
        }
        
        $this->smarty->display('frontend/subs/viewed_product');
    }

    public function ip_address() {
        echo '<center><h1>Your IP Address Is: ' . $this->input->ip_address() . '</h1></center>';
    }

    public function cpanel() {
        redirect(site_url('auth/cpanel/login'));
    }

}
/* End of file auth/cpanel.php */
/* Location: ./system/application/controllers/auth/cpanel.php */